package handler

import (
	"web_framework/model"
	"web_framework/pkg/constvar"
	. "web_framework/pkg/e"
	"web_framework/storage"
)

//登录身份验证
type GetDeviceInfoHandle struct {
}

func (getDeviceInfo GetDeviceInfoHandle) handleMsg(data model.Request, clientId string) (result model.Response) {
	if data.Mtype == constvar.MtypeAppGetJoinDeviceInfoRequest {
		//检查参数是否为空
		if len(data.Data.Mac) == 0 || len(data.Data.Sn) == 0 {
			Log("Info", "mac/sn参数异常")
			return ReturnErr(data.Mid, constvar.MtypeAppGetJoinDeviceInfoResponse, "mac/sn参数异常")
		}

		deviceUUID := Md5Str(data.Data.Sn + data.Data.Mac)
		deviceInfoCacheKey := constvar.RouterRedisPrefix + data.Data.Sn + storage.RedisJoinDeviceInfoKey + ":" + deviceUUID
		res, err := storage.RedisHGetAll(deviceInfoCacheKey)
		if err != nil {
			Log("Info", "获取任务设备详情缓存失败")
			return ReturnErr(data.Mid, constvar.MtypeAppGetJoinDeviceInfoResponse, "获取任务设备详情缓存失败")
		}
		var upspeed string
		var dspeed string
		if speed ,ok := res["upspeed"].(string);ok{
			upspeed = speed
		}
		if speed ,ok := res["dspeed"].(string);ok{
			dspeed = speed
		}
		var returnData interface{} = model.ResponseData{
			UpSpeed:   upspeed,
			DownSpeed: dspeed,
		}

		return model.Response{
			Mid:    data.Mid,
			Mtype:  constvar.MtypeAppGetJoinDeviceInfoResponse,
			Info:   "获取连接设备速度信息成功",
			Status: constvar.StatusOk,
			Data: &returnData,
		}
	}
	Log("Info", "获取失败 210")
	return ReturnErr(data.Mid, constvar.MtypeAppGetJoinDeviceInfoResponse, "获取失败")
}
